package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.User;
import com.example.domain.edit.NewPassword;
import com.example.domain.paging.PageHelper;
import com.example.domain.query.UserQuery;
import com.example.mapper.UserMapper;
import com.example.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-17
 */
@Service
@Qualifier("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean save(User user) {
        return userMapper.save(user);
    }

    @Override
    public List<User> list() {
        return userMapper.list();
    }

    @Override
    public boolean modify(User user) {
        return userMapper.updateById(user)>0;
    }

    @Override
    public boolean delete(Integer id) {
        return userMapper.delete(id);
    }


    public IPage<User> getPage(Integer currentPage, Integer pageSize) {
        IPage page =new Page(currentPage,pageSize);
        userMapper.selectPage(page,null);
        return page;
    }

    @Override
    public IPage<User> getPage(Integer currentPage, Integer pageSize, User user) {
        LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<User>();
        lqw.like(Strings.isNotEmpty(user.getUsername()),User::getUsername,user.getUsername());
        lqw.like(Strings.isNotEmpty(user.getPhone()),User::getPhone,user.getPhone());
        lqw.like(Strings.isNotEmpty(user.getPassword()),User::getPassword,user.getPassword());
        lqw.like(Strings.isNotEmpty(user.getAddress()),User::getAddress,user.getAddress());
        IPage page =new Page(currentPage,pageSize);
        userMapper.selectPage(page,lqw);
        return page;
    }

    @Override
    public List<User> queryUsers(String username, String phone, Integer status) {
        return lambdaQuery()
                .like(username !=null,User::getUsername,username)
                .like(phone !=null,User::getPhone,phone)
                .eq(status !=null,User::getStatus,status)
                .list();
    }

    @Override
    public int editPassword(NewPassword newPassword) {
        return userMapper.editPassword(newPassword);
    }


    @Override
    public List<User> findAll(PageHelper pageHelper) {
        return userMapper.findAll(pageHelper);
    }

}
